package com.sup.servlet;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sup.entity.Project;
import com.sup.entity.User;

/**
 * Servlet implementation class ShowProjectServlet
 */
@WebServlet("/showProject")
public class ShowProjectServlet extends HttpServlet {
	private static EntityManagerFactory emf;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowProjectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		emf = Persistence.createEntityManagerFactory("PersistenceUnit");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EntityManager em = emf.createEntityManager();
		
		String idProject = request.getParameter("id");
		
		Query infoProject = em.createQuery("SELECT Project FROM Project project WHERE idProject="+idProject);
		Project project = (Project) infoProject.getSingleResult();
		
		Query infoUser = em.createQuery("SELECT User from User user WHERE idUser="+project.getUser().getIdUser());
		User user = (User) infoUser.getSingleResult();
		
		request.setAttribute("user", user);
		request.setAttribute("project", project);
		request.getRequestDispatcher("/showProject.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
